#!/bin/bash  
#  
# tomcat: Start/Stop/Restart tomcat  
# author: xuc
# chkconfig: 2345 80 20  
# description: Tomcat is a Java Servlet Container  
#  
#  
# match these values to your environment:  
export CATALINA_BASE=/home/tomcat/apache-tomcat-7.0.37
export CATALINA_HOME=/home/tomcat/apache-tomcat-7.0.37
export CATALINA_TMPDIR=/home/tomcat/apache-tomcat-7.0.37/temp
export JRE_HOME=/home/java/jdk1.7.0_79
export CLASSPATH=/home/tomcat/apache-tomcat-7.0.37/bin/bootstrap.jar:/home/tomcat/apache-tomcat-7.0.37/bin/tomcat-juli.jar
export JAVA_HOME=/home/java/jdk1.7.0_79
tomcat_e=/var/lock/subsys/tomcat

RETVAL=0
prog="tomcat7-9100"

# Source function library.  
. /etc/rc.d/init.d/functions  

# Source networking configuration.
. /etc/sysconfig/network

TOMCAT_PROCESS_STR=`ps aux | grep 'java.*tomcat' | grep -v grep`
PROCESS_ARRAY=(${TOMCAT_PROCESS_STR// / })
TOMCAT_PROCESS_ID=${PROCESS_ARRAY[1]}


start(){  
	if [ -e $tomcat_e ];then
		echo -n "$prog: allready running"
		exit 1
	fi

	echo -n $"Starting $prog: " 
	echo -e "\n"
        $CATALINA_HOME/bin/startup.sh  
	touch $tomcat_e
            
}  

stop(){  
       
	echo -n $"Stopping $prog: "

	for ((a=1;a<=100;a++))
	do
		CHECK_STR_LENGTH=0
		TOMCAT_PROCESS_CHECK_STR=`ps aux | grep 'java.*tomcat' | grep -v grep`
		CHECK_STR_LENGTH=${#TOMCAT_PROCESS_CHECK_STR}
		
		if [ $CHECK_STR_LENGTH != 0 ]
		then
			#kill $TOMCAT_PROCESS_ID
			$CATALINA_HOME/bin/shutdown.sh
			sleep 2
			echo -n .
		else
			
			break
		fi
	
	done
	echo -e "\n"
	rm -f $tomcat_e

}

  
case "$1" in  
start)  
    start  
    ;;  
stop)  
    stop  
    ;;  
restart)  
    stop 
    start  
    ;; 
*)  
    echo -n $"Usage: $0 {start|stop|restart}"  
    ;;  
esac  
exit $RETVAL